package com.soft.javase.ch09.set;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author wangzhisen
 * @Desc TODO
 * @date 2025/5/6
 */
public class UserTreeSetDemo {
    public static void main(String[] args) {

        TreeSet<User> set = new TreeSet(new Comparator<User>() {

            @Override
            public int compare(User o1, User o2) {
                //按照年龄降序进行排序，若年龄相同则按照姓名的升序进行排列
                if(o1.getAge() == o2.getAge()){
                    return o1.getUsername().compareTo(o2.getUsername());
                }

                return o2.getAge() - o1.getAge();
            }
        });

        set.add(new User("aaa",18 ));
        set.add(new User("abc",31 ));
        set.add(new User("aba",32 ));
        set.add(new User("abc",31 ));
        set.add(new User("abe",18 ));
        set.forEach(System.out::println);
    }
}
